package one.stand.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.jmp.base.LabelApi;
import com.jmp.base.MessageApi;
import com.jmp.base.dto.MessagePushRequest;
import com.jmp.base.dto.MessagePushType;
import com.jmp.feign.MessageFeign;
import one.stand.advice.ResultEnum;
import one.stand.mapper.GoodsGroupBuyingMapperExt;
import one.stand.model.GoodsGroupBuying;
import one.stand.service.GoodsGroupService;
import one.stand.service.sys.UserContentService;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import one.stand.util.SqlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class GoodsGroupServiceImpl implements GoodsGroupService {

    @Autowired
    private MessageFeign messageFeign;

    @Autowired
    private GoodsGroupBuyingMapperExt goodsGroupBuyingMapperExt;
    @Autowired
    private UserContentService userContentService;

    @Override
    public void save(GoodsGroupBuying goodsGroupBuying) {
        if (Objects.nonNull(goodsGroupBuying.getId())) {//update
            goodsGroupBuying.setStatus(null);
            goodsGroupBuying.setUpdateTime(new Date());
            goodsGroupBuyingMapperExt.updateByPrimaryKeySelective(goodsGroupBuying);
        } else {//insert
            goodsGroupBuying.setStatus(STATUS_DOWN);
            goodsGroupBuying.setCreateTime(new Date());
            goodsGroupBuyingMapperExt.insertSelective(goodsGroupBuying);
        }
    }

    @Override
    @Transactional
    public void up(Integer id, List<Integer> labelIds, String pushTitle, String pushContent) {
        GoodsGroupBuying goodsGroupBuyingSearch = get(id);
        AssertUtil.isFalse(goodsGroupBuyingSearch.getStatus() == STATUS_UP, ResultEnum.FAIL, "已发布无法再发布");
        AssertUtil.nonNull(goodsGroupBuyingSearch.getGoodsId(), ResultEnum.PARAM_CHECK, "拼图商品为空时无法发布");
        AssertUtil.nonNull(goodsGroupBuyingSearch.getEndTime(), ResultEnum.PARAM_CHECK, "结束时间为空时无法发布");

        Date now = new Date();

        GoodsGroupBuying goodsGroupBuying = new GoodsGroupBuying();
        goodsGroupBuying.setId(id);
        goodsGroupBuying.setStatus(STATUS_UP);
        if (Objects.isNull(goodsGroupBuyingSearch.getStartTime())) {
            goodsGroupBuying.setStartTime(now);
        }
        goodsGroupBuying.setUpdateTime(now);
        goodsGroupBuyingMapperExt.updateByPrimaryKeySelective(goodsGroupBuying);

        //bind labels
        Integer[] labels = OneListUtil.isNotEmpty(labelIds) ? ArrayUtil.toArray(labelIds, Integer.class) : new Integer[]{LabelApi.LBAEL_ID_ALL};
        userContentService.updateLabel(LabelApi.LABEL_TYPE_GOODS_GROUP, id, labels);

        // push
        if (StringUtils.isNoneBlank(pushTitle)) {
            if (OneListUtil.isNotEmpty(labelIds)) {//标签
                MessagePushRequest pushDto = MessagePushRequest.builder()
                        .type(MessagePushType.GOODS_GROUP)
                        .toType(MessageApi.TYPE_LABELS_COMPANY)
                        .toIds(labelIds)
                        .title(pushTitle)
                        .fillData(new String[]{pushContent})
                        .build();
                messageFeign.push(pushDto);
            } else {//所有
                MessagePushRequest pushDto = MessagePushRequest.builder()
                        .type(MessagePushType.GOODS_GROUP)
                        .toType(MessageApi.TYPE_ALL)
                        .title(pushTitle)
                        .fillData(new String[]{pushContent})
                        .build();
                messageFeign.push(pushDto);
            }
        }
    }

    @Override
    @Transactional
    public void down(Integer id) {
        GoodsGroupBuying goodsGroupBuying = new GoodsGroupBuying();
        goodsGroupBuying.setId(id);
        goodsGroupBuying.setStatus(STATUS_DOWN);
        goodsGroupBuying.setUpdateTime(new Date());
        goodsGroupBuyingMapperExt.updateByPrimaryKeySelective(goodsGroupBuying);

        //unbind labels
        userContentService.updateLabel(LabelApi.LABEL_TYPE_GOODS_GROUP, id, null);
    }

    @Override
    public void remove(Integer id) {
        GoodsGroupBuying goodsGroupBuyingSearch = get(id);
        AssertUtil.isFalse(goodsGroupBuyingSearch.getStatus() == STATUS_UP, ResultEnum.FAIL, "上架无法删除");

        GoodsGroupBuying goodsGroupBuying = new GoodsGroupBuying();
        goodsGroupBuying.setId(id);
        goodsGroupBuying.setStatus(STATUS_REMOVE);
        goodsGroupBuying.setUpdateTime(new Date());
        goodsGroupBuyingMapperExt.updateByPrimaryKeySelective(goodsGroupBuying);
    }

    @Override
    public GoodsGroupBuying get(Integer id) {
        return goodsGroupBuyingMapperExt.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsGroupBuying> getList() {
        return goodsGroupBuyingMapperExt.selectList();
    }

    @Override
    public List<GoodsGroupBuying> getList(Integer companyId) {
        List<Integer> labelIds = userContentService.getList(LabelApi.LABEL_TYPE_COMPANY, companyId);
        labelIds.add(LabelApi.LBAEL_ID_ALL);
        String labelIdsStr = SqlUtil.in(labelIds);
        return goodsGroupBuyingMapperExt.selectViewList(LabelApi.LABEL_TYPE_GOODS_GROUP, labelIdsStr);
    }
}
